# coding=UTF-8 
from django.shortcuts import render
from django import forms
from django.forms import ModelForm
from django.http import HttpResponse
from user_login.models import User_login
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.core.mail import send_mail

#create form
class LoginForm(forms.Form):
   username = forms.CharField(label = '用户名',max_length = 50)
   password = forms.CharField(label = '密码',max_length = 50)
   studyID = forms.CharField(label = '学号',max_length = 13,required = False)

# Create your views here.
def regist(request):
    registf = LoginForm(request.POST)     #get the post form
    if registf.is_valid():
        #get cleaned data
        username = registf.cleaned_data['username']
        password = registf.cleaned_data['password']
        studyID = registf.cleaned_data['studyID']
        #write into mysql
        user1 = User.objects.create(username = username,password = password)
        User_login.objects.create(user = user1,studyID = studyID) 
        req=1
        return HttpResponse(req)
    else:
        registf = LoginForm()        #get empty form
    return render_to_response('information.html',{'uf':registf})

def login(request):
    loginf = LoginForm(request.POST)
    if loginf.is_valid():
        username = loginf.cleaned_data['username']
        password = loginf.cleaned_data['password']
        user = User_login.objects.filter(username = username,password = password)

        if  user:
            return HttpResponse('Login  success.')
        else:
            return HttpResponse('Login failed.')
    else:
        loginf = LoginForm()
    return render_to_response('login.html',{'uf':loginf})

def setEmail(request):
    if request.method == 'POST':
        #method 1:
        #send_mail('subject','this is the message of email','czgrs2010@163.com',fail_silently = True)
        
        #method 2:
        message1 = ('subject1','this is the message of email1','czgrs2010@163.com')